package ru.royalgatemagazine.afisha;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public abstract class AbstractView extends FrameLayout {
	
	Debug mDebug = new Debug(this) {
		protected boolean isDBG() {
			return true;
		}
	};
	
	public AbstractView(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(layout(), this);
	}
	
	public AbstractView(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(layout(), this);
	}

	public AbstractView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		LayoutInflater.from(context).inflate(layout(), this);
	}
	
	protected abstract int layout();
	
	public void setViewBackground(int id, int res){
		View v = findViewById(id);
		if(v!=null){
			v.setBackgroundResource(res);
		}
		else{
			mDebug.log("setViewBackground(int id, int res), v==null, res_id: "+res);
		}
	}
	
	public void setViewBackground(int id, Drawable d){
		View v = findViewById(id);
		if(v!=null){
			v.setBackgroundDrawable(d);
		}
		else{
			mDebug.log("setViewBackground(int id, Drawable d), v==null");
		}
	}
	
	public void setImageResource(int id, int res){
		ImageView v = (ImageView) findViewById(id);
		if(v!=null){
			v.setImageResource(res);
		}
		else{
			mDebug.log("setImageResource(int id, int res), v==null, res_id: "+res);
		}
	}
	
	public void setImageDrawable(int id, Drawable d){
		ImageView v = (ImageView) findViewById(id);
		if(v!=null){
			v.setImageDrawable(d);
		}
		else{
			mDebug.log("setImageResource(int id, Drawable d), v==null");
		}
	}
	
	public CharSequence getText(int id){
		CharSequence text = "";
		TextView v = (TextView) findViewById(id);
		if(v!=null){
			text = v.getText();
		}
		else{
			mDebug.log("setText(), v==null, res_id: "+id);
		}
		return text;
	}
	
	public void setText(int id, int res){
		TextView v = (TextView) findViewById(id);
		if(v!=null){
			v.setText(res);
		}
		else{
			mDebug.log("setText(), v==null, res_id: "+res);
		}
	}
	
	public void setText(int id, String text){
		TextView v = (TextView) findViewById(id);
		if(v!=null && text!=null){
			v.setText(text);
		}
		else{
			mDebug.log("setText(), v==null || string==null, string: "+text);
		}
	}
	
	public void setTextSize(int id, int size){
		TextView v = (TextView) findViewById(id);
		if(v!=null && size>0){
			v.setTextSize(TypedValue.COMPLEX_UNIT_DIP, size);
		}
		else{
			mDebug.log("setTextSize(), v==null || size<0, size: "+size);
		}
	}
	
	public void setTextColor(int id, int color){
		TextView v = (TextView) findViewById(id);
		if(v!=null && color>0){
			v.setTextColor(color);
		}
		else{
			mDebug.log("setTextColor(), v==null || color<0, size: "+color);
		}
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getView(int id, Class<T> c){
		if(c==null || id<0){throw new IllegalArgumentException();}
		return (T)findViewById(id);
	}
	
	public ListView getListView(){
		ListView lv = (ListView) findViewById(android.R.id.list);
		if(lv==null)throw new IllegalStateException("There is no view with id android.R.id.list");
		return lv;
	}
	
	public ListView getListView(int id){
		ListView lv = (ListView) findViewById(id);
		if(lv==null)throw new IllegalStateException("There is no view with id: "+id);
		return lv;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getController(Class<T> c){
		if(c==null){throw new IllegalArgumentException();}
		return (T)getContext();
	}
}
